import cache from "../cache.js"
import defaultData from "../../data/defaultData"
import download from "./download"
import mode from "../../data/mode.js";

let target = defaultData.api + '/file/upload/'

// import * as imageConversion from 'image-conversion';


export default {

	fileLimit(file, maxSize = 2) {

		let size = file.size / 1048576


		if (size > maxSize)
			return `大小不能超过${maxSize}M`

		return true
	},

  //压缩
  async compress(file,size = 500){
	  
	    
		   // let res = await imageConversion.compressAccurately(file, size)
	  
		   
		   let newFile = new File([res], 'filename', {
			   type: res.type,
			   lastModified: Date.now()
		   })
		
		   
		   return newFile
	   

   },
	uploadFile: async (data) => {
		let {
			type,
			classify,
			file,
			filePath,
			name
		} = data
		



		let url = target + type + '/' + classify
		let token = uni.getStorageSync(cache.loginToken)


		return new Promise((resolve, reject) => {

			uni.uploadFile({
				url,
				filePath: filePath,
				method: 'POST',
				name,
				header: {
					token

				},
				formData: {
					'user': 'test'
				},
				success: function(res) {
					resolve(JSON.parse(res.data))
				},
				error: function(error) {

					reject(error)
				}
			})
		})

	},




}